package net.easyjava.third.letv.util;


import com.alibaba.fastjson.JSONObject;
import net.easyjava.tools.http.HttpTools;
import net.easyjava.tools.security.md5.Md5Tools;
import org.apache.commons.net.util.Base64;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.text.Collator;
import java.util.*;

/**
 * Created by ivan on 15/4/28.
 */
@Deprecated
public class LetvVideoSource {

    private static Map<String, String> parameter = new HashMap<String, String>();

    private static String apiurl = "http://api.letvcloud.com/open.php";

    private static String vrvideoplayurl = "http://api.letvcloud.com/getplayurl.php";

    private static String user_unique = "53f80d6851";
    private static String format = "json";
    private static String ver = "2.0";
    private static String secretkey = "249ce738ccbcdc78c787c25118e377df";

    private static String video_upload_init_api = "video.upload.init";
    private static String video_update_api = "video.update";
    private static String video_list_api = "video.list";
    private static String video_get_api = "video.get";

    public static void setup() {
        parameter.put("user_unique", user_unique);
        parameter.put("timestamp", System.currentTimeMillis() + "");
        parameter.put("format", format);
        parameter.put("ver", ver);
    }

    public LetvVideoSource() {
        setup();
        if (parameter.containsKey("sign")) {
            parameter.remove("sign");
        }
    }

    @Deprecated
    public JSONObject videoUploadInit(String video_name, String client_ip, Integer file_size, Integer Ispay) throws Exception {
        parameter.put("api", video_upload_init_api);
        parameter.put("video_name", encoder(video_name));
//        parameter.put("client_ip",client_ip);
//        parameter.put("file_size",file_size.toString());
//        parameter.put("Uploadtype","0");
//        parameter.put("isdrm","0");
//        parameter.put("Isdownload","1");
//        parameter.put("Ispay",Ispay.toString());

        String sign = generateSign();
        parameter.put("sign", sign);
        String rtn = HttpTools.post(apiurl, null, parameter);
        JSONObject returnObject = JSONObject.parseObject(rtn);
        //System.out.println(returnObject.toString());
        return returnObject;//.getJSONObject("data");
    }

    public JSONObject videoUpdate(String video_id, String video_name, String video_desc, String tag, String is_pay, String Video_url) throws Exception {
        parameter.put("api", video_update_api);
        parameter.put("video_id", video_id);
        parameter.put("video_name", encoder(video_name));
        parameter.put("video_desc", encoder(video_desc));
        parameter.put("tag", encoder(tag));
//        parameter.put("is_pay",is_pay);
//        parameter.put("Video_url",Video_url);

        String sign = generateSign();
        parameter.put("sign", sign);
        String rtn = HttpTools.post(apiurl, null, parameter);
        JSONObject returnObject = JSONObject.parseObject(rtn);
        return returnObject;//.getJSONObject("data");
    }

    public JSONObject videoList(Integer index, Integer size, Integer status) throws Exception {
        parameter.put("api", video_list_api);
        parameter.put("index", index.toString());
        parameter.put("size", size.toString());
        parameter.put("status", status.toString());

        String sign = generateSign();
        parameter.put("sign", sign);
        String rtn = HttpTools.post(apiurl, null, parameter);
        JSONObject returnObject = JSONObject.parseObject(rtn);
        return returnObject;//.getJSONObject("data");
    }

    @Deprecated
    public JSONObject videoGet(String video_id) throws Exception {
        parameter.put("api", video_get_api);
        parameter.put("video_id", video_id);

        String sign = generateSign();
        parameter.put("sign", sign);
        String rtn = HttpTools.post(apiurl, null, parameter);
        JSONObject returnObject = JSONObject.parseObject(rtn);
        return returnObject;//.getJSONObject("data");
    }

    public String vrvideourlGet(String video_id) throws Exception {
        parameter.put("user", user_unique);
        parameter.put("video", video_id);
        parameter.put("vtype", "mp4");
        parameter.put("ts", System.currentTimeMillis() + "");

        String sign = generateSign();
        parameter.put("sign", sign);
        String rtn = HttpTools.post(vrvideoplayurl, null, parameter);
        JSONObject returnObject = JSONObject.parseObject(rtn);
        JSONObject videoList = returnObject.getJSONObject("data").getJSONObject("video_list");
        System.out.println(videoList);
        String main_url = null;
        if (videoList.containsKey("video_4")) {
            JSONObject yuanhua = videoList.getJSONObject("video_4");
            main_url = new String(Base64.decodeBase64(yuanhua.getString("main_url")));
        } else {
            if (videoList.containsKey("video_3")) {
                JSONObject chaoqing = videoList.getJSONObject("video_3");
                main_url = new String(Base64.decodeBase64(chaoqing.getString("main_url")));
            } else {
                JSONObject gaoqing = videoList.getJSONObject("video_2");
                main_url = new String(Base64.decodeBase64(gaoqing.getString("main_url")));
            }
        }
        return main_url;//.getJSONObject("data");
    }

    @Deprecated
    private String generateSign() {
        Comparator comparator = Collator.getInstance(Locale.getDefault());
        String[] keys = new String[parameter.keySet().size()];
        int index = 0;
        for (String key : parameter.keySet()) {
            keys[index] = key;
            index++;
        }
        Arrays.sort(keys, comparator);
        String readyTosign = "";
        for (String rk : keys) {
            readyTosign += rk + parameter.get(rk);
        }
        return Md5Tools.md5(readyTosign + secretkey);
    }

    private String encoder(String str) throws UnsupportedEncodingException {
        return URLEncoder.encode(str, "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        LetvVideoSource lvs = new LetvVideoSource();
        //ceshi
        // System.out.println(lvs.videoUploadInit("测试",null,null,null).getString("upload_url"));

//        System.out.println(lvs.vrvideourlGet("46772b04a1"));
    }

}
